package com.panshi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.dao.ISearchDao;
import com.panshi.domain.Classify;
import com.panshi.domain.Search;
import com.panshi.domain.Users;
import com.panshi.service.IClassifyService;
import com.panshi.service.ISearchService;
import com.panshi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchService implements ISearchService {

    @Autowired
    private ISearchDao iSearchDao;

    // 用户
    @Autowired
    private IUserService iUserService;

    // 分类
    @Autowired
    private IClassifyService iClassifyService;

    @Override
    public PageInfo<Search> queryListSearch(String keyword, Integer page, Integer pageSize) {
        // 分页
        PageHelper.startPage(page, pageSize);
        // 关键字搜索
        List<Search> searchList = iSearchDao.queryListSearch(keyword);
        // 对所有数据进行详细查询
        for (Search search: searchList  ) {
            // 通过搜索的用户 id ，查询出用户信息
            Users users = iUserService.querySearchUser(search.getUserId());
            search.setUsers(users);
            // 通过收藏表 id， 查询收藏信息
            Classify classify = iClassifyService.querySearchClassify(search.getClassifyId());
            search.setClassify(classify);
        }
        // 将数据存放进分页内
        PageInfo<Search> pageInfo = new PageInfo<>(searchList);
        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public void addSearch(Search search) {
        iSearchDao.addSearch(search);
    }


}
